﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using KingOfCSharp.iHunter;

namespace KingOfCSharp.iHunter
{
    /// <summary>
    /// Represent a message item
    /// </summary>
    public sealed class Message : IMessage, IEquatable<IMessage>
    {
        private IDictionary<string, string> customFields;
        private static Random rand = new Random();

        public Message()
        {
            
            Attachments = new List<IAttachment>();
            customFields = new Dictionary<string, string>();
            this.Title = string.Empty;
            this.Category = string.Empty;
            this.PublishTime = DateTime.MinValue;
            this.UpdateTime = DateTime.MinValue;
            this.Author = string.Empty;
            this.Content = string.Empty;
            this.Folder = null;
            this.ChannelGuid = Guid.Empty;
            this.ReplyGuid = Guid.Empty;
            this.HashCode = 0;
            this.Read = true;
            this.ViewerGuid = Guid.Empty;
            this.EditorGuid = Guid.Empty;
            this.ReplyGuid = Guid.Empty;
            this.Receiver = string.Empty;
        }
       

        public Message(Guid guid, int hashCode)
            :this()
        {
            this.Guid = guid;
            this.HashCode = hashCode;
        }

        public Message(Guid guid, IMessage source)
            :this()
        {
            this.Guid = guid;
            this.Title = source.Title;
            this.Category = source.Category;
            this.PublishTime = source.PublishTime;
            this.UpdateTime = source.UpdateTime;
            this.Author = source.Author;
            this.Receiver = source.Receiver;
            this.Content = source.Content;
            this.ChannelGuid = source.ChannelGuid;
            this.Read = source.Read;
            this.ReplyGuid = source.ReplyGuid;
            this.ViewerGuid = source.ViewerGuid;
            this.EditorGuid = source.EditorGuid;
            this.Folder = source.Folder;
            this.HashCode = source.HashCode;
            this.OriginalFolderGuid = source.OriginalFolderGuid;

            this.customFields = new Dictionary<string, string>();

            foreach (KeyValuePair<string,string> field in source.CustomFields)
            {
                this.CustomFields.Add(field);
            }

            foreach (IAttachment attachment in source.Attachments)
            {
                this.Attachments.Add(new Attachment(attachment.Guid, attachment));
            }
        }

 
        public Guid Guid
        {
            get;
            set;
        }

         #region IMessage Members

      
        public string Title { get; set; }
        public Guid ChannelGuid { get; set; }
        public DateTime PublishTime { get; set; }
        public DateTime UpdateTime { get; set; }
        public string Author { get; set; }
        public string Category { get; set; }
        public string Content { get; set; }
        public Guid ReplyGuid { get; set; }
        public bool Read { get; set; }
        public IFolder Folder { get; set; }
        public ICollection<IAttachment> Attachments
        {
            get;
            private set;
        }
        public IDictionary<string, string> CustomFields
        {
            get { return customFields; }
        }


        public string Receiver
        {
            get;
            set;
        }

      
        public Guid ViewerGuid
        {
            get;
            set;
        }

        public Guid EditorGuid
        {
            get;
            set;
        }

        public int HashCode
        {
            get;
            private set;
        }

        public Guid OriginalFolderGuid
        {
            get;
            set;
        }

        #endregion

        public override int GetHashCode()
        {
            return HashCode;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            return Equals(obj as IMessage);
        }

        #region IEquatable<IMessage> 成员

        public bool Equals(IMessage other)
        {
            if (other == null)
                return false;

            if (other.HashCode != this.HashCode)
                return false;

            //TODO: Compare other fields

            return true;
        }

        #endregion

    }
}